/**
 * Created by tanbingqin on 2020/1/17.
 */
import React from 'react';
import { connect } from 'react-redux';
import { messages, deepFullCopy } from 'share/common'
import { Alert, Row, Col, Switch, Divider } from 'antd';
import Chooser from 'components/chooser'
import chooserData from 'share/chooserData'

class ItineraryExpense extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      expenseSelectedList: [] //已选初始值
    }
  }

  componentDidMount() {
    const { value } = this.props;
    this.setState({expenseSelectedList: value || []});
  };

  componentWillReceiveProps(nextProps) {
    this.setState({expenseSelectedList: nextProps.value || []});
  };

  handleSelectExpense = (value) => {
    const { onChange } = this.props;
    this.setState({expenseSelectedList: value});
    if (onChange) {
      onChange(value);
    }
  };

  render() {
    const { fatherExpenseType, expenseTypeCategory, setOfBooksId, disabled } = this.props;
    const { expenseSelectedList } = this.state;
    let chooserItem = deepFullCopy(chooserData['available_expense']);
    let selectItem = {
      type: 'select',
      id: 'parentId',
      label: messages('request-2.key5')/*所属父类型*/,
      options: fatherExpenseType
    };
    let selectItem2 = {
      type: 'select',
      id: 'expenseTypeCategoryId',
      label: messages('request-2.key6')/*费用大类*/,
      options: expenseTypeCategory
    };
    chooserItem.searchForm.push(selectItem);
    chooserItem.searchForm.push(selectItem2);
    return (
      <div>
        <Chooser
          placeholder={messages('common.select')/*请选择*/}
          selectorItem={chooserItem}
          labelKey='name'
          valueKey='expenseTypeOID'
          listExtraParams={{
            setOfBooksId: setOfBooksId || undefined,
            enabled: true,
            levelCode: 'ALL'
          }}
          disabled={disabled}
          onChange={this.handleSelectExpense}
          value={expenseSelectedList}
          single/>
      </div>
    )
  }
}

ItineraryExpense.propTypes = {
  disabled: React.PropTypes.bool,  //是否可编辑
  onChange: React.PropTypes.func,  //数据变化的回调
  fatherExpenseType: React.PropTypes.array,
  expenseTypeCategory: React.PropTypes.array,
  setOfBooksId: React.PropTypes.string
};

ItineraryExpense.defaultProps = {
  disabled: false,
  onChange: () => {},
  fatherExpenseType: [],
  expenseTypeCategory: [],
  setOfBooksId: ''
};

function mapStateToProps(state) {
  return {

  }
}

export default connect(mapStateToProps)(ItineraryExpense)
